<template>
  <div class="app-container">
    <!-- 搜索 -->
    <el-card class="box-card">
      <el-row>
        <el-col :span="12">
          <div class="grid-content bg-purple search">
            <el-form ref="query" :model="query" label-width="100px">
              <el-form-item style="margin-bottom:0px">
                <i slot="label" class="el-icon-search" style="font-size:20px"></i>
                <el-input v-model="query.value" placeholder="手机"></el-input>
              </el-form-item>
            </el-form>
          </div>
        </el-col>

        <el-col :span="12">
          <div class="grid-content bg-purple-light" style="text-align:right">
            <el-button type="warning" @click="getConsumerList">刷新</el-button>
            <el-button type="primary" @click="queryAsk">查询</el-button>
            <el-button @click="queryReset">重置</el-button>
          </div>
        </el-col>
      </el-row>
    </el-card>
    <!-- 表格 -->
    <el-card class="box-card">
      <img
        class="watermark"
        v-if="baseApi== 'https://testouhun.wm2177.com'"
        style="left: 0;"
        src="@/assets/watermark.png"
      />
      <el-table
        @sort-change="sortChange"
        :data="tableData"
        :header-cell-style="{background:'#eee'}"
        border
        v-loading="loading"
        style="width: 100%;margin-bottom: 20px;"
      >
        <el-table-column align="center" type="index" width="50" label="序号" fixed></el-table-column>
        <el-table-column align="center" label="头像" fixed>
          <template slot-scope="scope">
            <el-popover placement="top-start" title trigger="hover">
              <img :src="scope.row.himg" alt style="max-width:150px" />
              <!-- <el-avatar shape="square" :size="150" fit="cover" :src="scope.row.img"></el-avatar> -->
              <el-avatar
                slot="reference"
                shape="square"
                :size="50"
                fit="scale-down"
                :src="scope.row.himg"
              ></el-avatar>
              <!-- <img
                slot="reference"
                :src="scope.row.img"
                style="width: 60px;height: 60px"
              />-->
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="nickname" label="用户信息" width="120px" fixed>
          <template slot-scope="scoped">
            <div>{{scoped.row.nickname}}</div>
            <div>{{scoped.row.name}}</div>
            <div>{{scoped.row.mobile}}</div>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="balance" label="余额" sortable="custom"></el-table-column>
        <el-table-column align="center" prop="total_flow_wx" label="累计无限流水" sortable="custom"></el-table-column>
        <el-table-column align="center" prop="total_flow_normal" label="累计普通流水" sortable="custom"></el-table-column>
        <el-table-column align="center" prop="total_flow" label="累计总流水" sortable="custom"></el-table-column>
        <el-table-column align="center" prop="total_recharge" label="累计充值" sortable="custom"></el-table-column>
        <el-table-column align="center" prop="total_give" label="累计赠送" sortable="custom"></el-table-column>
        <el-table-column align="center" prop="total_bag" label="背包总价值" sortable="custom"></el-table-column>
        <el-table-column align="center" prop="nickname" label="上级信息" width="120px">
          <template slot-scope="scoped">
            <div>{{scoped.row.pnickname}}</div>
            <div>{{scoped.row.pmobile}}</div>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="add_time" label="注册时间"></el-table-column>
        <el-table-column align="center" fixed="right" label="操作" width="100">
          <template slot-scope="scope">
            <el-button @click="handleEdite(scope.row)" type="text" size="small">查看</el-button>

            <el-popconfirm
              confirmButtonText="好的"
              cancelButtonText="不用了"
              icon="el-icon-info"
              iconColor="red"
              title="是否确认修改头像？"
              @onConfirm="editUser(scope.row.id,'himg')"
            >
              <el-button
                slot="reference"
                style="margin-left:10px;color:red"
                type="text"
                size="small"
                :disabled="scope.row.status!=1"
              >修改头像</el-button>
            </el-popconfirm>

            <el-popconfirm
              confirmButtonText="好的"
              cancelButtonText="不用了"
              icon="el-icon-info"
              iconColor="red"
              title="是否确认修改昵称？"
              @onConfirm="editUser(scope.row.id,'nickname')"
            >
              <el-button
                slot="reference"
                style="margin-left:10px;color:red"
                type="text"
                size="small"
                :disabled="scope.row.status!=1"
              >修改昵称</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <div style="text-align:right">
        <el-pagination
          background
          layout="prev, pager, next"
          :current-page="currentPage"
          :total="total"
          @current-change="currentChange"
        ></el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
import { consumerList, editUserInfo, newUserList } from '@/api/consumer'
import { parseTime } from '@/utils/index'

export default {
  name: 'ConsumerList',
  data() {
    return {
      baseApi: process.env.VUE_APP_BASE_API,
      // 分页数据
      total: 0,
      currentPage: 1,
      pageSize: 10,
      // 表数据
      tableData: [],
      // 筛选
      query: {
        value: ''
      },
      loading: false
    }
  },
  mounted() {
    this.getConsumerList()
  },
  methods: {
    //获取用户列表
    async getConsumerList() {
      this.loading = true
      const res = await newUserList({
        limit: this.pageSize,
        page: this.currentPage,
        keywords: this.query.value,
        order_by: this.query.order_by
      })
      this.loading = false
      // console.log(res);
      this.total = res.count
      this.tableData = res.data
      this.tableData.forEach((item) => {
        if (item.add_time == 0) {
          item.add_time = '-'
        } else {
          item.add_time = parseTime(item.add_time, '{y}-{m}-{d} {h}:{i}:{s}')
        }
        if (item.last_time == 0) {
          item.last_time = '-'
        } else {
          item.last_time = parseTime(item.last_time, '{y}-{m}-{d} {h}:{i}:{s}')
        }
      })
    },
    //分页改变
    currentChange(current) {
      this.currentPage = current
      this.getConsumerList()
    },
    queryAsk() {
      this.getConsumerList()
    },
    queryReset() {
      this.total = 0
      this.currentPage = 1
      this.pageSize = 10
      this.query.value = ''
      this.resetForm('query')
      this.getConsumerList()
    },
    // 表格操作
    handleEdite(row) {
      this.$router.push({
        path: '/Consumer/ConsumerOprate',
        query: { id: row.id }
      })
    },
    // 修改用户
    async editUser(id, type) {
      const res = await editUserInfo({
        field: type,
        user_id: id
      })
      if (res.st == 1) {
        this.getConsumerList()
        this.$message({
          message: '修改成功',
          type: 'success'
        })
      } else {
        this.$message.error('修改失败')
      }
    },
    // 重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    // 排序
    sortChange({ column, prop, order }) {
      if (order == 'descending') {
        this.query.order_by = prop + ',' + 'desc'
        this.getConsumerList()
      } else if (order == 'ascending') {
        this.query.order_by = prop + ',' + 'asc'
        this.getConsumerList()
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.box-card {
  margin-bottom: 20px;
}
.search {
  display: flex;
  align-items: center;
}
::v-deep .el-input--medium .el-input__inner {
  width: 360px;
}
::v-deep .el-drawer.rtl {
  width: 650px !important;
  overflow: auto;
}
</style>
